<?php
/**
 * 2021/3/18
 * Created by breeze
 * User: breeze
 */

namespace app\admin\controller;


use app\admin\business\adminBus;
use app\admin\business\contentBus;
use app\Request;
use think\facade\View;

class Content extends AdminBase
{
    public function index(){
        return View::fetch();
    }

    /**获取content列表
     * @return \think\response\Json
     */
    public function contentList(){
        $contentBus = new contentBus();
        try {
            $result = $contentBus->getContentList();
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$result);
    }
    public function add(){
        return View::fetch();
    }

    /**
     * 添加content
     * @return \think\response\Json
     */
    public function addContent(){
        $title = $this->request->param('title');
        $content = $this->request->param('content');
        $data = [
            'title'=>$title,
            'content'=>$content
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('content_add')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $res = $contentBus->addContent($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }

        return show(config('status.success'),'添加成功',$res);
    }
    public function detail(Request $request){
        $id =$request->param('id');
        View::assign("id",$id);
        return View::fetch();
    }

    /**获取content详情
     * @return \think\response\Json
     */
    public function getContentDetail(){
        $id = $this->request->param('id');
        $data = [
            'id'=>$id
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('category_find')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $result = $contentBus->getContentDetail($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        if(empty($result)){
            return show(config('status.error'),'数据被删除');
        }
        return show(config('status.success'),'查询成功',$result);
    }

    /**
     * 更新content内容
     * @return \think\response\Json
     */
    public function updateContent(){
        $id = $this->request->param('id');
        $title = $this->request->param('title');
        $content = $this->request->param('content');
        $data = [
            'id'=>$id,
            'title'=>$title,
            'content'=>$content
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('content_update')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $res = $contentBus->updateContent($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }

        return show(config('status.success'),'添加成功',$res);
    }

    /**
     * 删除文章
     * @return \think\response\Json
     */
    public function removeContent(){
        $id = $this->request->param("id");
        $data = [
            'id' => $id
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('content_remove')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $result = $contentBus->removeContent($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'删除成功',$result);
    }
    public function changeSort(){
        $id = $this->request->param('id');
        $sort = $this->request->param('sort');
        $data = [
            'id'=>$id,
            'sort'=>$sort
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('content_sort')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $res = $contentBus->changeContent($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }

        return show(config('status.success'),'更改成功',$res);
    }

    /**获取类别列表
     * @return string
     */
    public function categoryList(){
        return View::fetch('categoryList');
    }
    public function getCategoryList(){
        $contentBus = new contentBus();
        try {
            $result = $contentBus->getCategoryList();
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$result);
    }
    public function categoryAdd(){
        return View::fetch('categoryAdd');
    }

    /**
     * 添加类别
     * @return \think\response\Json
     */
    public function addCategory(){
        $name =$this->request->param('name',"","trim");
        $date = [
            'category_name'=>$name
        ];

        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('category_add')->check($date)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $result = $contentBus->addCategory($name);
        } catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
//        if(empty($result)){
//            return show(config('status.error'),'添加失败');
//        }
        return show(config('status.success'),'添加成功',$result);
    }


    public function categoryDetail(Request $request){
        $id =$request->param('id');
        View::assign("id",$id);
        return View::fetch('categoryDetail');
    }

    /**
     * 获取类别详情
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function getCategoryDetail(){
        $id =$this->request->param('id');
        $data = [
            'id'=>$id
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('category_find')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $result = $contentBus->getDetail($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        if(empty($result)){
            return show(config('status.error'),'数据被删除');
        }
        return show(config('status.success'),'查询成功',$result);
    }

    /**
     * 更新类别
     * @return \think\response\Json
     */
    public function updateCategory(){
        $id =$this->request->param('id');
        $category_name =$this->request->param('category_name');
        $data = [
            'id'=>$id,
            "category_name" =>$category_name
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('category_update')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $result = $contentBus->updateCategory($data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'修改成功',$data);
    }

    public function removeCategory(){
        $id = $this->request->param("id");
        $data = [
            'id' => $id
        ];
        $validate = new \app\admin\validate\Content();
        if(!$validate->scene('category_find')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $contentBus = new contentBus();
        try {
            $result = $contentBus->removeCategory($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'删除成功',$result);
    }

}